#include "addimage.h"

AddImage::AddImage(QString last_dir_)
{
setupUi(this);

image_file = QLatin1String("");
image_path = QLatin1String("");
data_dir = QLatin1String("");
last_dir = QLatin1String("");
    if ( true != last_dir_.isEmpty() )
    {
    last_dir = last_dir_;
    }

formats = get_formats();

//QPushButton * okButton = qobject_cast<QPushButton *>( buttonBox->button( QDialogButtonBox::Ok ) );
//    if (okButton != NULL)
//    {
//    okButton->setDisabled(true);
//    }

QObject::connect( imFileToolButton,
                  SIGNAL( clicked() ),
                  this,
                  SLOT( file_name() ) );
QObject::connect( imSaveAsComboBox,
                  SIGNAL( currentIndexChanged(const QString &) ),
                  this,
                  SLOT( type_changed(const QString &) ) );
}

AddImage::~AddImage()
{
}

void AddImage::file_name()
{
QString image_formats = tr("Images");
image_formats.append( QLatin1String(" ") );
image_formats.append(formats);

QString path = QFileDialog::getOpenFileName( this,
                                             tr("Select image file"),
                                             last_dir,
                                             image_formats );

QString file_path = QDir::toNativeSeparators(path);
last_dir = file_path.section( QDir::separator(), 0, -2 );

image_path = QDir::toNativeSeparators(path);
image_file = image_path.section( QDir::separator(), -1 );
last_dir = image_path.section( QDir::separator(), 0, -2 );

fLineEdit->setText(image_path);

}

QMap<QString, QString> AddImage::return_selection()
{
QMap<QString, QString> m;
m["file_path"] = fLineEdit->text();
    if ( m.value("file_path").isEmpty() )
    {
    QMessageBox::warning( this,
                          tr("Error"),
                          tr("Invalid path") );      
    }

m["file_name"] = image_file.trimmed();

    if ( imSaveAsComboBox->currentText() == QString( QLatin1String("LINK") ) )
    {
    m["file_type"] = QString( QLatin1String("LINK") );
    }
    else if ( imSaveAsComboBox->currentText() == QString( QLatin1String("BLOB") ) )
    {
    m["file_type"] = QString( QLatin1String("BLOB") );
    }
    else if ( imSaveAsComboBox->currentText() == QString( QLatin1String("URL") ) )
    {
    m["file_type"] = QString( QLatin1String("URL") );
    }

    if ( sizeGroupBox->isEnabled() && sizeGroupBox->isChecked() )
    {
    m["resol"] = imSizeComboBox->currentText();
    }
    else
    {
    m["resol"] = QLatin1String("original");
    }

m["last_im_dir"] = last_dir;

return m;    
}

void AddImage::type_changed( const QString & type )
{
    if ( type == QString( QLatin1String("LINK") ) )
    {
    sizeGroupBox->setDisabled(true);
    imFileToolButton->setEnabled(true);
    }
    else if ( type == QString( QLatin1String("URL") ) )
    {
    sizeGroupBox->setDisabled(true);
    imFileToolButton->setDisabled(true);
    }
    else if ( type == QString( QLatin1String("BLOB") ) )
    {
    sizeGroupBox->setEnabled(true);
    imFileToolButton->setEnabled(true);
    }
}

QString AddImage::get_formats()
{
QList<QByteArray> f = QImageReader::supportedImageFormats();
QString l = QLatin1String("");
QString l2 = QLatin1String("");
    for (int i = 0; i< f.size(); ++i)
    {
        if ( i != f.size() - 1 )
        {
        l.append( QLatin1String("*.") );
        l.append( QString( f.at(i) ) );
        l.append( QLatin1String("  ;;") );
        l2.append( QLatin1String("*.") );
        l2.append( QString( f.at(i) ) );
        l2.append( QLatin1String(" ") );
        }
        else
        {
        l.append( QLatin1String("*.") );
        l.append( QString( f.at(i) ) );
        l.append( QLatin1String("") );
        l2.append( QLatin1String("*.") );
        l2.append( QString( f.at(i) ) );
        l2.append( QLatin1String(";;") );
        }
    }
l2.append(l);
return l2;
}
